﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 矿井信息管理-树结构
    /// </summary>
    /// Title:ZMCS40
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张志芳   [创建]
    ///
    public partial class ZMCS40 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        public ZMCS40()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCS40_Loaded);
        }

        /// <summary>
        /// 加载树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCS40_Loaded(object sender, RoutedEventArgs e)
        {
            this.mineModelTree.Refresh();
        }

        /// <summary>
        /// 选择树节点，右侧页面刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mineModelTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.mineModelTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = this.GetTreeNodeInfo(item);
            string level = args["level"];

            switch (level)
            {
                case "0":
                    //维护矿井信息
                    this.container.LoadView("ZMCS41", args);
                    break;
                case "1":
                    //维护生产系统
                    this.container.LoadView("ZMCS42", args);
                    break;
                case "2":
                    //维护生产单元
                    this.container.LoadView("ZMCS43", args);
                    break;
                case "3":
                    //维护变量组 & 变量
                    args.Add("fdParentTable", "T_CI_PRODUCTIONUNITS");
                    this.container.LoadView("ZMCS44", args);
                    break;
                case "4":
                    //维护变量
                    args.Add("fdParentTable", "T_CI_VARIABLEGROUP");
                    this.container.LoadView("ZMCS45", args);
                    break;
                default:
                    //变量Details
                    this.container.LoadView("ZMCS46", args);
                    break;
            }
        }

        /// <summary>
        /// ZMCS44页面的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnViewInitialized(object sender, ViewInitializedEventArgs e)
        {
            Dictionary<string, string> args = this.GetTreeNodeInfo(item);
            EFView view = (sender as EFView);
            view.GetType().GetField("fdParentId").SetValue(view, args["fdParentId"]);
            view.GetType().GetField("fdParentTable").SetValue(view, "T_CI_PRODUCTIONUNITS");
        }


        /// <summary>
        /// 获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree">从EFTree获取节点</param>
        /// <returns></returns>
        private Dictionary<string, string> GetTreeNodeInfo(EFTreeItem treeItem)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();

            string _value = treeItem.ItemObject.GetType().GetProperty("value").GetValue(treeItem.ItemObject, null) as string;
            args.Add("fdParentId", _value);

            _value = treeItem.ItemObject.GetType().GetProperty("level").GetValue(treeItem.ItemObject, null) as string;
            args.Add("level", _value);

            _value = (treeItem.IsLeaf == true) ? "1" : "0";
            args.Add("isLeaf", _value);

            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value;
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("value").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("level").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("level", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;
                    args.Add("level", _value);
                }
            }
            BaseTools.RefreshTree("ZMCS40", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.mineModelTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.mineModelTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;

            if (item.IsLeaf)
            {
                //叶子节点
                EFTreeItem parent = item.Parent as EFTreeItem;
                string node = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();

                item = BaseTools.FindTreeItem(parent.Items, node, "value") as EFTreeItem;
                this.mineModelTree.SelectedItem = item;
                return;
            }

            //非叶子节点
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mineModelTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0)
            {
                this.mineModelTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }
    }
}
